package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BlockModel;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChoiceTopicComplexView extends LinearLayout {
    private Context context;
    private LinearLayout layRight;
    private FrameLayout layContent;
    private int index = 0;
    private Map<Integer, View> cache = new HashMap<>();
    private EventBean eventData;

    public ChoiceTopicComplexView(Context context) {
        super(context);
    }

    public ChoiceTopicComplexView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicComplexView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicComplexView(Context context, ChoiceTopicBean bean) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_complex_view, null);
        layRight = view.findViewById(R.id.layRight);
        layContent = view.findViewById(R.id.svContent);
        eventData = bean.getEventData();
        this.context = context;
        setLayoutRight(bean.getBlocks(),bean);
        updateUI(bean);
        addView(view);
    }


    private void updateUI(ChoiceTopicBean bean) {
        layContent.removeAllViews();
        if (cache.containsKey(index)) {
            layContent.addView(cache.get(index));
        }else{
            View content = new ChoiceTopicOtherPagerView(context,bean.getBlocks().get(index).getList(),bean.getNovelList(),index,eventData);
            cache.put(index, content);
            layContent.addView(content);
        }
    }

    private void setTextViewColor(TextView textView, boolean isSelect) {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            if (isSelect) {
                textView.setTextColor(getResources().getColor(R.color.theme_main_dark));
            }else{
                textView.setTextColor(getResources().getColor(R.color.text_dark));
            }
        }else{
            if (isSelect) {
                textView.setTextColor(getResources().getColor(R.color.text_color_57));
            }else{
                textView.setTextColor(getResources().getColor(R.color.text_color_7));
            }
        }
    }

    private void setLayoutRight(List<BlockModel> list,final ChoiceTopicBean bean) {
        if (list == null) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            BlockModel blockModel = list.get(i);
            TextView textView = new TextView(context);
            setTextViewColor(textView,index == i);

            textView.setText(" "+blockModel.getTitle()+" ");
            final int tempIndex = i;
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_TR_TAB,eventData);
                    index = tempIndex;
                    for (int j = 0; j < layRight.getChildCount(); j++) {
                        TextView tv = (TextView) layRight.getChildAt(j);
                        setTextViewColor(tv,index == j/2&& j%2==0);

                    }
                    updateUI(bean);
                }
            });
            if (i > 0) {
                TextView tvLine = new TextView(context);
                tvLine.setText("|");
                setTextViewColor(tvLine,false);
                layRight.addView(tvLine);
            }
            layRight.addView(textView);
        }
    }

}
